//-----------------------------------//
//-----------------------------------//

#include <stdio.h>
#include <time.h>
#include <string.h>
#include "mq_mgr.h"

#include <errno.h>

//-----------------------------------//
//-----------------------------------//

int mq_init(struct mq_mgr_t *p_mq_mgr, const char *p_mq_name,int mode, int msg_max, int msgsize_max)
{
    struct mq_attr attriute;

    //-------------------
    //check params
    if (p_mq_mgr == NULL || p_mq_name == NULL || strlen(p_mq_name) <= 0 || msg_max < 0 || msgsize_max < 0 )
    {
        return -1;
    }

    //-------------------
    //open mq,create it if not exists
    memset(&attriute, 0, sizeof(attriute));
    attriute.mq_maxmsg = msg_max;
    attriute.mq_msgsize = msgsize_max;

    //p_mq_mgr->m_mq_fd = mq_open(p_mq_name, O_RDWR|O_CREAT, mode, &attriute);
    p_mq_mgr->m_mq_fd = mq_open(p_mq_name, O_RDWR|O_CREAT|O_NONBLOCK, mode, &attriute);
    if (p_mq_mgr->m_mq_fd < 0)
    {
    	printf("errno:%d,strerror:%s\n", errno, strerror(errno));

    	p_mq_mgr->m_inited = 0;
        return -1;
    }

    //-------------------
    //init successfully
    p_mq_mgr->m_inited = 1;
    return 0;
}

//-----------------------------------//
//-----------------------------------//

int mq_send_msg(struct mq_mgr_t *p_mq_mgr, const void *p_data,int data_len,int msg_priority)
{
    //-------------------
    //check module status
    if(!p_mq_mgr->m_inited)
    {
        return -1;
    }

    //-------------------
    //param check
    if(NULL == p_data || data_len < 0)
    {
        return -1;
    }

    //-------------------
    //send msg to mq
    if(mq_send(p_mq_mgr->m_mq_fd,(char*)p_data,data_len,msg_priority))
    {
        return -1;
    }
    else
    {
        return 0;
    }
}

//-----------------------------------//
//-----------------------------------//

int mq_send_msg_timedout(struct mq_mgr_t *p_mq_mgr, const void* p_data,int data_len,int msg_priority,int seconds)
{
    struct timespec time_interval;

    //-------------------
    //check module status
    if(!p_mq_mgr->m_inited)
    {
        return -1;
    }

    //-------------------
    //param check
    if(NULL == p_data || data_len < 0 || seconds < 0)
    {
        return -1;
    }

    //-------------------
    //send msg with timedout
    memset(&time_interval,0,sizeof(time_interval));
    time_interval.tv_sec = time(NULL) + seconds;

    if(mq_timedsend(p_mq_mgr->m_mq_fd,(char*)p_data,data_len,msg_priority,&time_interval))
    {
        //print_in_hex((char*)p_data, data_len);
        return -1;
    }
    else
    {
        return 0;
    }
}

//-----------------------------------//
//-----------------------------------//

int mq_send_msg_timedout2(struct mq_mgr_t *p_mq_mgr, const void* p_data,int data_len,int msg_priority,int milliseconds)
{
    struct timespec time_interval;

    //-------------------
    //check module status
    if(!p_mq_mgr->m_inited)
    {
        return -1;
    }

    //-------------------
    //param check
    if(NULL == p_data || data_len < 0 || milliseconds < 0)
    {
        return -1;
    }

    //-------------------
    //send msg with timedout
    memset(&time_interval,0,sizeof(time_interval));
    time_interval.tv_sec = time(NULL);
    time_interval.tv_nsec = milliseconds * 1000000;

    if(mq_timedsend(p_mq_mgr->m_mq_fd,(char*)p_data,data_len,msg_priority,&time_interval))
    {
        //print_in_hex((char*)p_data, data_len);
        return -1;
    }
    else
    {
        return 0;
    }
}

//-----------------------------------//
//-----------------------------------//

int mq_recv_msg(struct mq_mgr_t *p_mq_mgr, char* p_recv_buffer,int buffer_len,unsigned int* p_msg_priority)
{
    //-------------------
    //check module status
    if(!p_mq_mgr->m_inited)
    {
        return -1;
    }

    //-------------------
    //check params
    if(NULL == p_recv_buffer || buffer_len < 1)
    {
        return -1;
    }

    return mq_receive(p_mq_mgr->m_mq_fd,p_recv_buffer,buffer_len,p_msg_priority);
}

//-----------------------------------//
//-----------------------------------//

int mq_recv_msg_timedout(struct mq_mgr_t *p_mq_mgr, char* p_recv_buffer,int buffer_len,unsigned int* p_msg_priority,int seconds)
{
    struct timespec time_interval;

    //-------------------
    //check module status
    if(!p_mq_mgr->m_inited)
    {
        return -1;
    }

    //-------------------
    //check params
    if(NULL == p_recv_buffer || buffer_len < 1 || seconds < 0)
    {
        return -1;
    }

    //-------------------
    //recv msg with timedout
    memset(&time_interval,0,sizeof(time_interval));
    time_interval.tv_sec = time(NULL) + seconds;

    return mq_timedreceive(p_mq_mgr->m_mq_fd,p_recv_buffer,buffer_len,p_msg_priority,&time_interval);
}

//-----------------------------------//
//-----------------------------------//

int mq_uninit(struct mq_mgr_t *p_mq_mgr)
{
    //-------------------
    //check module status
    if(!p_mq_mgr->m_inited)
    {
        return -1;
    }

    //-------------------
    //close mq
    mq_close(p_mq_mgr->m_mq_fd);
    p_mq_mgr->m_mq_fd = -1;
    p_mq_mgr->m_inited = 0;

    return 0;
}

//-----------------------------------//
//-----------------------------------//
